package com.example.rabbit;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA.
 * ClassName: Consumer
 * Package: com.example.rabbit
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-06-08
 * Time: 20:16
 */

public class Consumer {

    //消费者 消费消息
    public static final String QUEUE_USER = "user";

    //接收消息
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.86.131");
        factory.setUsername("admin");
        factory.setPassword("123");
        //创建连接 产生信道
        Connection connection = factory.newConnection();
        //接收消息
        Channel channel = connection.createChannel();

        //回调 声明接收消息
        DeliverCallback deliverCallback = (consumerTag, message) ->{
            System.out.println(new String(message.getBody()));
        };

        //取消消息的回调
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("消费消息中断失败的回调函数");
        };

        /**
         * 消费者接收消息
         * 1.消费哪个队列
         * 2.消除成功之后是否要自动应答 true 代表自动应答
         * 3.消费者没有成功收到消息的回调函数 lambda表达式
         * 4.消费者取消消费的回调
         */
        channel.basicConsume(QUEUE_USER,true,deliverCallback,cancelCallback);

    }


}
